//
// Created by yangchao on 2022/6/7.
//131. 分割回文串: https://leetcode.cn/problems/palindrome-partitioning/
//

#include <vector>
#include <string>
using namespace std;

//回溯+动态规划
class Partition {
private:
    vector<vector<int>> dp;
    vector<vector<string>> ret;
    vector<string> ans;
    int len;

public:
    vector<vector<string>> partition(string s) {
        len = s.size();
        dp.assign(len,vector<int>(len, true));

        for (int i = len-1; i >= 0 ; --i) {
            for (int j = i + 1; j < len; ++j) {
                dp[i][j] = (s[i] == s[j]) && dp[i+1][j-1];
            }
        }
        dfs(s,0);
        return ret;
    }

    void dfs(const string& s, int i) {
        if (i == len) {
            ret.push_back(ans);
            return;
        }
        for (int j = i; j < len; ++j) {
            if (dp[i][j]) {
                ans.push_back(s.substr(i,j-i+1));
                dfs(s,j+1);
                ans.pop_back();
            }
        }
    }
};

